package com.ruoyi.web.core.config;

import com.alibaba.fastjson.JSON;
import com.ruoyi.web.controller.tool.AuthenCheckServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;

@Component
public class Douyin implements HandlerInterceptor {
    @Autowired
    private AuthenCheckServer authenCheckServer;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean check = authenCheckServer.check(request);
        if (check) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            PrintWriter writer = response.getWriter();
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("success", "false");
            stringStringHashMap.put("msg", "抖音授权过期！");
            stringStringHashMap.put("code", "500");
            Object json = JSON.toJSON(stringStringHashMap);
            writer.append(json.toString());
        }
        return !check;
    }
}
